//
//  TodayViewController.m
//  BasketballClub
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "TodayViewController.h"
#import "LiveTableViewCell.h"
#import "LiveHandle.h"
#import "LiveModel.h"
#import <MJRefresh.h>
#import "WebViewController.h"
@interface TodayViewController ()<UITableViewDataSource,UITableViewDelegate,LiveTableViewCellDelegate>


///tableView
@property(nonatomic,strong)UITableView *todayTableView;

@end

@implementation TodayViewController



- (void)viewDidLoad {
    [super viewDidLoad];
   
    //设置tableView
    self.todayTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-148*LAYOUT_SIZE_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.todayTableView];
    //设置为代理
    self.todayTableView.delegate = self;
    self.todayTableView.dataSource = self;
    //注册cell
    [self.todayTableView registerClass:[LiveTableViewCell class] forCellReuseIdentifier:@"LiveTableViewCellToday"];
    [self upData];
}




#pragma mark - tableViewDataSoucre
//ROWS
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    return [LiveHandle sharedLiveHandle].liveTodayDataArray.count;
    
}

//cell的创建和重用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiveTableViewCellToday"];
    if ([LiveHandle sharedLiveHandle].liveTodayDataArray.count) {
        LiveModel *model = [LiveHandle sharedLiveHandle].liveTodayDataArray[indexPath.row];
        [cell setCellDataFromLiveModel:model];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    return cell;
}
#pragma mark  - cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190*LAYOUT_SIZE_HEIGHT;
}


#pragma mark - 代理方法 技术统计
///
-(void)LiveTableViewCellCountBtnDidClicked:(LiveTableViewCell *)cell
{
    NSIndexPath *indexPath = [self.todayTableView indexPathForCell:cell];
    LiveModel *liveModel = [LiveHandle sharedLiveHandle].liveTodayDataArray[indexPath.row];
    NSString *urlString = liveModel.link2url;
   
    [self jumpToWebViewWith:urlString];
 
    
}

#pragma mark  -代理方法 文字/视频直播方法
-(void)LiveTableViewCellVideoOrTextBtnDidClicked:(LiveTableViewCell *)cell
{
    NSIndexPath *indexPath = [self.todayTableView indexPathForCell:cell];
    
    LiveModel *liveModel = [LiveHandle sharedLiveHandle].liveTodayDataArray[indexPath.row];
    NSString *urlString = liveModel.m_link1url;
    
    [self jumpToWebViewWith:urlString];
    
}
#pragma mark - 跳转到webView
-(void)jumpToWebViewWith:(NSString *)str
{
    
    WebViewController *webViewVC =[[WebViewController alloc]init];
   
    webViewVC.url = [NSURL URLWithString:str];
   
    //push到WebView
    [self.navigationController pushViewController:webViewVC animated:YES];
    
}







#pragma mark - 下拉刷新
-(void)upData
{
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.todayTableView.mj_header = header;
    [self.todayTableView.mj_header beginRefreshing];
    
    
}
#pragma mark  - 刷新执行的方法
-(void)refreshAction
{
    [LiveHandle sharedLiveHandle].liveTodayDataArray = nil;
    __weak typeof(self) weakSlef = self;
    [[LiveHandle sharedLiveHandle] GetTodayDataFromNetWorkWithBlock:^(BOOL isFinished) {
        if (isFinished) {
            [weakSlef.todayTableView reloadData];
        }else{
            
            [[LiveHandle sharedLiveHandle] GetTodayDataFromNetWorkWithBlock:^(BOOL isFinished) {
                
            }];
        }
        [weakSlef.todayTableView.mj_header endRefreshing];
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [LiveHandle sharedLiveHandle].liveTodayDataArray = nil;
}





@end
